package com.wormhole.wormholemanagement.controller;

import com.wormhole.wormholemanagement.entity.MedicalInsuranceRecord;
import com.wormhole.wormholemanagement.service.MedicalInsuranceRecordService;
import com.wormhole.wormholemanagement.utils.SecurityUtils;
import com.wormhole.wormholemanagement.utils.SpecificationBuilder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/medical-records")
@Tag(name = "医保档案管理")
public class MedicalInsuranceRecordController {

    private final MedicalInsuranceRecordService recordService;

    @Autowired
    public MedicalInsuranceRecordController(MedicalInsuranceRecordService recordService) {
        this.recordService = recordService;
    }

    @PostMapping
    @Operation(summary = "创建医保档案")
    public ResponseEntity<MedicalInsuranceRecord> createRecord(@RequestBody MedicalInsuranceRecord record,
                                                               @RequestParam Long operatorId) {
        MedicalInsuranceRecord createdRecord = recordService.createRecord(record, operatorId);
        return ResponseEntity.ok(createdRecord);
    }

    @PostMapping("/batch")
    @Operation(summary = "批量创建医保档案")
    public ResponseEntity<List<MedicalInsuranceRecord>> createRecordsBatch(
            @RequestBody List<MedicalInsuranceRecord> records) {

        // 从Security上下文自动获取operatorId
        Long operatorId = SecurityUtils.getCurrentOperatorId();
        List<MedicalInsuranceRecord> createdRecords = recordService.createRecordsBatch(records, operatorId);
        return ResponseEntity.ok(createdRecords);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新医保档案")
    public ResponseEntity<MedicalInsuranceRecord> updateRecord(@PathVariable Long id,
                                                               @RequestBody MedicalInsuranceRecord record,
                                                               @RequestParam Long operatorId) {
        MedicalInsuranceRecord updatedRecord = recordService.updateRecord(id, record, operatorId);
        return ResponseEntity.ok(updatedRecord);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除医保档案")
    public ResponseEntity<Void> deleteRecord(@PathVariable Long id, @RequestParam Long operatorId) {
        recordService.deleteRecord(id, operatorId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取医保档案详情")
    public ResponseEntity<MedicalInsuranceRecord> getRecordById(@PathVariable Long id) {
        MedicalInsuranceRecord record = recordService.getRecordById(id);
        return ResponseEntity.ok(record);
    }

    @GetMapping
    @Operation(summary = "获取医保档案列表")
    public ResponseEntity<Page<MedicalInsuranceRecord>> getAllRecords(
            @PageableDefault(size = 10) Pageable pageable) {
        Page<MedicalInsuranceRecord> records = recordService.getAllRecords(pageable);
        return ResponseEntity.ok(records);
    }

    @GetMapping("/search")
    public ResponseEntity<Page<MedicalInsuranceRecord>> searchRecords(
            @RequestParam Map<String, String> params,
            @PageableDefault(size = 10) Pageable pageable) {

        Specification<MedicalInsuranceRecord> spec = SpecificationBuilder.buildSpecification(params);
        Page<MedicalInsuranceRecord> records = recordService.searchRecords(spec, pageable);
        return ResponseEntity.ok(records);
    }


}